#include <REGX52.H>
int count1=5000;
int count2=500;
int i;
bit a =1;
bit b =1;
int j;
void delay_ms(unsigned int ms)
{
    unsigned int i, j;
    for (i = 0; i < ms; i++)
    {
        for (j = 0; j < 123; j++)
            ;
    }
}
void K1()
{
  P2=0X01;
  P1=0x01;
  delay_ms(500);
  P2=0x00;
  P1=0x00;
  delay_ms(500);
}
void K2()
{
  P2=0x04;
  P1=0x04;
  delay_ms(500);
  P2=0x00;
  P1=0x00;
  delay_ms(500);  
}
void INT0_Init() {
    IT0 = 1;    
    EX0 = 1;    
    EA = 1;     
}

void INT1_Init() {
    IT1 = 1;    
    EX1 = 1;    
    EA = 1;     
}
void MORAL(int count1)
{
     P2=0X02;
     P1=0X01;
     for(j=0;j<=count1;j++)
     {
      if(P3_7==0)
      {
       count1 = 10000;
      }
      delay_ms(1);
     }
     count1 =5000;
     //delay_ms(count1);
     P2=0X04;
     P1=0X01;
     for(i=0;i<=6;i++)
    {
      if(P3_7==0)
      {
       count1 = 10000;
      }
      P2_2=~P2_2;
      delay_ms(500);
    }
    P2=0X01;
    P1=0X02;
   for(j=0;j<=count1;j++)
     {
      if(P3_7==0)
      {
       count1 = 10000;
      }
      delay_ms(1);
     }
     count1 =5000;
     P2=0X01;
     P1=0X04;
     for(i=0;i<=6;i++)
    {
      if(P3_7==0)
      {
       count1 = 10000;
      }
      P1_2=~P1_2;
      delay_ms(500);
    }
}
void main()
{
   INT0_Init();
   INT1_Init();
   while(1)
   {
    MORAL(count1);
   }
}
//按到红灯灭时在松开；
void INT0_ISR() interrupt 0
{
  EA=0;

  while(a)
  {
    K1();
    if(!P3_2)
    {
      a=0;
    }
}
  EA=1;
}
//按到黄灯灭时在松开
void INT1_ISR() interrupt 2
{
  EA=0;

  while(b)
  {
    K2();
    if(!P3_3)
    {
      b=0;
    }
  }
  EA=1;
}